﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using MyBookReadsList.Data;
using MyBookReadsList.Models;
using System.Windows.Input;
using MyBookReadsList.Commands;
using System.Windows.Controls;


namespace MyBookReadsList.ViewModels
{
    public class BooksListViewModel : ViewModelBase, IPageViewModel
    {
        private BookViewModel selectedBook;
        public BookViewModel SelectedBook
        {
            get
            {
                return this.selectedBook;
            }

            set
            {
                this.selectedBook = value;
                this.OnPropertyChanged("SelectedBook");
            }
            
        }

        private ObservableCollection<BookListViewModel> booksList;

        public string Name
        {
            get
            {
                return "Books";
            }
        }

        private ICommand selectionChange;
        private ICommand selectionBookChange;
        private ICommand addToShelf;

        public ICommand AddToShelf
        {
            get
            {
                if (this.addToShelf == null)
                {
                    this.addToShelf = new RelayCommand(this.HandleAddToShelfCommand);
                }
                return this.addToShelf;
            }
        }

        private Shelf selectedShelf;

        public Shelf SelectedShelf
        {
            get
            {
                return this.selectedShelf;
            }
            set
            {
                this.selectedShelf = value;
                this.OnPropertyChanged("SelectedShelf");
            }
        }

        private void HandleAddToShelfCommand(object parameter)
        {
            try
            {
                DataPersister.AddBookToShelf(this.SelectedBook, this.SelectedShelf);
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public ICommand SelectionBookChange
        {
            get
            {
                if (this.selectionBookChange == null)
                {
                    this.selectionBookChange = new RelayCommand(this.HandleSelectionBookChangeCommand);
                }
                return this.selectionBookChange;
            }
        }

        private void HandleSelectionBookChangeCommand(object parameter)
        {
            var args = parameter as SelectionChangedEventArgs;
            var selected = args.AddedItems;
            if (this.BooksList.Count() > 0)
            {
                this.ChangeBook(selected[0]);
            }    
        }

        public ICommand SelectionChange
        {
            get
            {
                if (this.selectionChange == null)
                {
                    this.selectionChange = new RelayCommand(this.HandleSelectionChangeCommand);
                }
                return this.selectionChange;
            }
        }

        private void HandleSelectionChangeCommand(object parameter)
        {
            var args = parameter as SelectionChangedEventArgs;
            var selected = args.AddedItems;
            if (this.Categorys.Count() > 0)
            {
                this.ChangeSelection(selected[0]);
            }
        }

        public BooksListViewModel()
        {
        }

        private ObservableCollection<CategorysViewModel> categorys;



        public IEnumerable<CategorysViewModel> Categorys
        {
            get
            {
                if (this.categorys == null)
                {
                    this.Categorys = DataPersister.GetCategorys();
                }
                return this.categorys;
            }
            set
            {
                if (this.categorys == null)
                {
                    this.categorys = new ObservableCollection<CategorysViewModel>();
                }
                foreach (var item in value)
                {
                    this.categorys.Add(item);
                }
            }
        }
        public CategorysViewModel SelectedCategory;

        public IEnumerable<BookListViewModel> BooksList
        {
            get
            {
                if (this.booksList == null)
                {
                    this.BooksList = DataPersister.GetBooksList(SelectedCategory);
                }
                return this.booksList;
            }
            set
            {
                if (this.booksList == null)
                {
                    this.booksList = new ObservableCollection<BookListViewModel>();
                }
                this.booksList.Clear();
                foreach (var item in value)
                {
                    this.booksList.Add(item);
                }
            }
        }

        public void ChangeBook(object book)
        {
            var current = book as BookListViewModel;
            var downloadBook = DataPersister.GetBookByTitle(current.Title);
            this.SelectedBook = downloadBook;
        }


        public void ChangeSelection(object store)
        {
            this.SelectedCategory = store as CategorysViewModel;
            this.BooksList = DataPersister.GetBooksList(SelectedCategory);
        }

        private ObservableCollection<Shelf> shelves;

        public IEnumerable<Shelf> Shelves
        {
            get
            {
                if (this.shelves == null)
                {
                    this.Shelves = DataPersister.GetShelves();
                }
                return this.shelves;
            }
            set
            {
                if (this.shelves == null)
                {
                    this.shelves = new ObservableCollection<Shelf>();
                }
                this.shelves.Clear();
                foreach (var item in value)
                {
                    this.shelves.Add(item);
                }
            }
        }
    }
}
